package com.useeinfo.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.module.Recruit;
import com.useeinfo.service.IRecruit;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by xianping on 2017/1/20.
 */
@Controller
@RequestMapping("/web/recruit")
public class RecruitAction {

    @Autowired
    private IRecruit recruitService;

    @RequestMapping(value = "/findRecruitList",produces ="text/html;charset=UTF-8")
    @ResponseBody
    public String findRecruitList(){
        System.out.println("----------findRecruit   start------------");

        List<Recruit> recruitList = recruitService.findRecruit();
        String str = JSON.toJSONString(recruitList);
        JSONArray jsonArray = JSON.parseArray(str);

        JSONObject jsonObject = new JSONObject();
		jsonObject.put("errorCode","0");
		jsonObject.put("errorMsg","成功");
        jsonObject.put("total",recruitList.size());
        jsonObject.put("rows",jsonArray);

        System.out.println("response string:" + jsonObject.toJSONString());
        System.out.println("----------findRecruit   end------------");
        return jsonObject.toJSONString();
    }

	@RequestMapping(value = "/findRecruitById",produces ="text/html;charset=UTF-8")
	@ResponseBody
	public String findRecruitTitleList(@RequestParam(value = "recruitId") String recruitId){
		System.out.println("----------findRecruitById   start------------");
		JSONObject jsonObject = new JSONObject();

		List<Recruit> recruitList = recruitService.findRecruitById(Long.parseLong(recruitId));
		if(null == recruitList || recruitList.size() ==0 ){
			jsonObject.put("errorCode","1");
			jsonObject.put("errorMsg","无此信息");
			return jsonObject.toJSONString();
		}
		jsonObject.put("recruit",recruitList.get(0));
		jsonObject.put("errorCode","0");
		jsonObject.put("errorMsg","成功");

		System.out.println("----------findRecruitById   end------------");
		return jsonObject.toJSONString();
	}
}
